package com.xja.friend.controller;

import com.xja.friend.entity.Chatuser;
import com.xja.friend.entity.Message;
import com.xja.friend.service.MessageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("mess")
public class MessageController {
    @Resource
    HttpServletRequest request;

    @Resource
    private MessageService messageService;

    @PostMapping("message")
    public Map<String,Object> add(@RequestBody Message message){
        Map<String,Object> map = new HashMap<>();
        try{
            //fromid为用户登录id
            //用户id
            Chatuser chatusers = (Chatuser) request.getSession().getAttribute("loginuser");
            message.setFromid(chatusers.getId());
            //默认状态为未读
            message.setStatus(0);
            message.setTime(new Date());
            message.setId(UUID.randomUUID().toString());
            messageService.insert(message);
            Message message1 = messageService.selectbyFromid(message.getToid());
            //回信息了修改状态
            message1.setStatus(1);
            messageService.updateByPrimaryKey(message1);
            map.put("status","true");
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        map.put("status","false");
        return map;
    }
}
